class CNF:
    #默认的构造函数
    def __init__(self, cnf: dict):
        ''' cnf: [str:list] '''
        #cnf是一个字典 key:value
        self.cnf = cnf
        self.S = list(cnf.keys())[0]

    def find_product(self, target: str):
        ''' find the productor  '''
        res = []
        print(target)
        for key, value in self.cnf.items():
            if target in value:
                res.append(key)
        return res

#用来处理输入的类
class Input:
    ''' my input class for reading the CNF '''

    def getDictItemFromInput(self, s: str):
        s = s.replace(' ', '')  # delete extra blank spacing
        temp = s.split("->")
        return temp[0], temp[1].split("|")

    def getInput(self):
        ''' return a dict CNF '''
        res = {}
        while True:
            try:
                s = input()
                dict_item = self.getDictItemFromInput(s)
                res[dict_item[0]] = dict_item[1]
            except:
                break
        return res

class CYK:
    #获取cnf
    def __init__(self, cnf: CNF):
        self.cnf = cnf
    def find(self, target: str):
        ''' judge if the target is in the CNF '''
        self.dp_mat = [[""]*len(target) for i in range(len(target))]
        print(self.dp_mat)
        # initialize
        for i in range(len(target)):
            self.dp_mat[i][i] = list(set(self.cnf.find_product(target[i])))

        for length in range(2, len(target) + 1):
            # i,j are the position in dp_mat
            for i in range(len(target) - length + 1):
                j = i + length - 1
                temp = []
                # split the sub_target
                for k in range(i, j):
                    list1 = self.dp_mat[i][k]
                    list2 = self.dp_mat[k+1][j]
                    temp += self.find_product(list1, list2)
                    # print("=====================================")
                    # print((list1,list2))
                    # print(self.find_product(list1,list2))
                    # print(temp)
                self.dp_mat[i][j] = list(set(temp))

        if self.cnf.S in self.dp_mat[0][len(target) - 1]:
            return True
        return False

    def find_product(self, list1: list, list2: list):
        ''' find all productions from Cartesian product of two lists '''
        res = []
        for i in range(len(list1)):
            for j in range(len(list2)):
                temp = self.cnf.find_product(list1[i]+list2[j])
                if temp != None:
                    res += temp
        return res


print("Please Input the CNF: (The First Character must be Start symbol until Ctrl+Z )")
#创建一个Input类型的对象
myinput = Input()
# get the CNF read until miss ctrl + Z
data_dict = myinput.getInput()
print(data_dict)
cyk = CYK(CNF(data_dict))
print("input the string: (until Ctrl+Z)")
while True:
    try:
        target_str = input()
        if cyk.find(target_str):
            print("Yes")
        else:
            print("No")
        # visual = Visualization(cyk.dp_mat)
        # try:
        #     visual.seeAsCsv("result.csv")
        # except:
        #     print("Write Error: Please Close Last csv file")
    except:
        break